﻿
#include "stdafx.h"
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>

using namespace cv;
using namespace std;


Mat colorInvert(Mat srcImage);
void createMask(Mat srcImage, char type);
vector<Mat> masks, channels(3),merges(3);


int main()
{
	Mat srcImage, mergeImage_B, mergeImage_R, mergeImage_G, mergeImage;

	srcImage = imread("4孩200.tif");
	if (!srcImage.data)  { printf("读取错误！ \n"); }
	mergeImage = colorInvert(srcImage);
	split(srcImage, channels);//分离色彩通道	
	createMask(srcImage, 0);

	channels[0].copyTo(merges[0], masks[0]);
	channels[1].copyTo(merges[1], masks[1]);
	channels[2].copyTo(merges[2], masks[2]);



	mergeImage_B = merges.at(0);
	mergeImage_G = merges.at(1);
	mergeImage_R = merges.at(2);

	merge(merges, mergeImage);

	//vector<int> compression_params;
	//compression_params.push_back(CV_IMWRITE_PNG_COMPRESSION); //PNG格式图片的压缩级别  
	//compression_params.push_back(0);  //这里设置保存的图像质量级别
	//imwrite("out.png", mergeImage, compression_params);
	imwrite("out.tif", mergeImage);

	waitKey(0);
	return 0;
}
void createMask(Mat srcImage , char type)
{
	//vector<Mat> 的012 对应 BGR
	vector<Mat> templet(3),negative_45(3), positive_45(3), Y_shape(3), parallel(3);
	Mat mask_R, mask_G, mask_B;
	//Mat negative_45_R, negative_45_G, negative_45_B;
	//Mat positive_45_R, positive_45_G, positive_45_B;

	//type 1 == -45
	negative_45[2] = Mat::eye(cv::Size(3, 3), CV_8UC1);  // 对角线为1的对角矩阵  R
	negative_45[1] = (Mat_<uchar>(3, 3) << 0, 0, 1, 1, 0, 0, 0, 1, 0);  //G
	negative_45[0] = (Mat_<uchar>(3, 3) << 0, 1, 0, 0, 0, 1, 1, 0, 0);  //B

	//type 2 == +45
	positive_45[2] = (Mat_<uchar>(3, 3) << 0, 0, 1, 0, 1, 0, 1, 0, 0);	//R
	positive_45[1] = (Mat_<uchar>(3, 3) << 1, 0, 0, 0, 0, 1, 0, 1, 0);	//G
	positive_45[0] = (Mat_<uchar>(3, 3) << 0, 1, 0, 1, 0, 0, 0, 0, 1);	//B

	Y_shape[2] = (Mat_<uchar>(3, 3) << 1, 0, 1, 0, 1, 0, 0, 0, 0);	//R
	Y_shape[1] = (Mat_<uchar>(3, 3) << 0, 0, 0, 1, 0, 1, 0, 1, 0);	//G
	Y_shape[0] = (Mat_<uchar>(3, 3) << 0, 1, 0, 0, 0, 0, 1, 0, 1);	//B

	parallel[2] = (Mat_<uchar>(3, 3) << 1, 1, 1, 0, 0, 0, 0, 0, 0);	//R
	parallel[1] = (Mat_<uchar>(3, 3) << 0, 0, 0, 1, 1, 1, 0, 0, 0);	//G
	parallel[0] = (Mat_<uchar>(3, 3) << 0, 0, 0, 0, 0, 0, 1, 1, 1);	//B


	if (type == 1) templet = negative_45;
	else if (type == 2) templet = positive_45;
	else if (type == 3) templet = Y_shape;
	else if (type == 0) templet = parallel;

	else cout << "选择模板错误 " << endl;

	//cout << "negative_45_R = " << endl << negative_45_R << endl << endl;
	mask_R = repeat(templet[2], 1 + srcImage.rows / 3, 1 + srcImage.cols / 3);
	mask_R = mask_R.colRange(1, srcImage.cols + 1).clone();
	mask_R = mask_R.rowRange(1, srcImage.rows + 1).clone();

	mask_G = repeat(templet[1], 1 + srcImage.rows / 3, 1 + srcImage.cols / 3);
	mask_G = mask_G.colRange(1, srcImage.cols + 1).clone();
	mask_G = mask_G.rowRange(1, srcImage.rows + 1).clone();

	mask_B = repeat(templet[0], 1 + srcImage.rows / 3, 1 + srcImage.cols / 3);
	mask_B = mask_B.colRange(1, srcImage.cols + 1).clone();
	mask_B = mask_B.rowRange(1, srcImage.rows + 1).clone();

	masks.push_back(mask_B);
	masks.push_back(mask_G);
	masks.push_back(mask_R);

}

Mat colorInvert(Mat srcImage)
{
	// 完成反色功能 255-原像素
	Mat img_copy;
	srcImage.copyTo(img_copy);
	for (int j = 0; j<img_copy.rows; j++){
		uchar * row = img_copy.ptr<uchar>(j);

		for (int i = 0; i<(img_copy.cols * img_copy.channels()); i++)
			row[i] = 255 - row[i];
		
	}
	return img_copy;
}

